package Mr_11_20;

public class rotate {
    public static void main(String[] args) {
        String str1 = "ABCDE";
        String str2 = "CDEAB";
        //比较 方法：字符串截取放右
        boolean flag = compare(str1,str2);
        System.out.println(flag);
        //比较 方法：转换为字符数组，再调整顺序
        boolean flag1 = arrRotateCompare(str1,str2);
        System.out.println(flag1);

    }
    public static boolean compare(String str1,String str2){
        for (int i = 0; i < str1.length(); i++) { //每旋转一次比较一次，旋转次数为str1的长度；
            str1 = Rotate(str1);
            System.out.println(str1);
            if (str1.equals(str2)){
                return true;
            }
        }return false;
    }
    public static String Rotate(String str1){ ////str1形参为 ：旋转前，返回值为 ：旋转后
        char first = str1.charAt(0);
        String end = str1.substring(1);
        return end+first;
    }
    //数组旋转
    public static String arrRotate(String str1){
        char[] arr = str1.toCharArray();//把字符串 to 字符数组
        char first = arr[0];
        //['A','B','C']
        //[ 0,  1,  2 ]
        for (int i = 0; i < str1.length()-1; i++) {  //srt1.langth=3
            arr[i] = arr[i+1];
        }
        arr[arr.length-1] = first;
        String result = new String(arr);//字符数组 to 字符串
        return result;
    }
    public static boolean arrRotateCompare(String str1,String str2){
        for (int i = 0; i < str1.length(); i++) {
            str1 = arrRotate(str1);
            if (str1.equals(str2)){
                return true;
            }
        }return false;
    }
}
